﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Liang.Device.Camera.Dll;
using Liang.Device.Model.Camera;

namespace Liang.Device.Camera.Action
{
    /// <summary>
    /// 重庆港宇摄像头
    /// </summary>
    public sealed class CamyuCamera : ICamera<Camyu>
    {
        /// <summary>
        /// 静态只读实例，创建对象实例的唯一地方，加上只读属性可保证线程安全
        /// </summary>
        private static readonly CamyuCamera _instance = new CamyuCamera();

        /// <summary>
        /// 获取当前对象实例的唯一入口
        /// </summary>
        public static CamyuCamera Instance
        {
            get { return _instance; }
        }

        /// <summary>
        /// 判断摄像头是否已初始化SDK
        /// </summary>
        /// 
        private static bool isSDKInited = false;

        public static bool IsSDKInited
        {
            get { return CamyuCamera.isSDKInited; }
            set { CamyuCamera.isSDKInited = value; }
        }

        /// <summary>
        /// 静态构造函数，在这里初始化摄像头SDK，仅调用一次
        /// </summary>
        static CamyuCamera()
        {
            try
            {
                CamyuCamera.IsSDKInited = CamyuDll.KsCamera_Camyu_Init();
            }
            catch (Exception e)
            {
                throw new CameraException("无法初始化摄像头SDK", e.Message);
            }
        }

        private CamyuCamera()
        {

        }

        ~CamyuCamera()
        {
            if (IsSDKInited)
            {
                try
                {
                    CloseSnapServer();

                    CamyuDll.KsCamera_Camyu_Cleanup();
                }
                catch (Exception e)
                {
                    throw new CameraException("无法注销摄像头SDK", e.Message);
                }
            }
        }

        public bool Open(Camyu cameraParam)
        {
            bool ret = false;

            try
            {
                cameraParam.DevHandle = CamyuDll.KsCamera_Camyu_Login(cameraParam.Ipaddr, cameraParam.ControlPort, (ushort)cameraParam.Port);

                if (IntPtr.Zero == cameraParam.DevHandle)
                {
                    return false;
                }
                else
                {
                    ret = true;
                }

                if (cameraParam.RealCapture)
                {
                    // 实时抓拍，需要开启抓拍监听服务器
                    // 监听服务反复调用，只会开启一次
                    // 但抓拍监听端口只会在第一次开启服务时被设定
                    ret = OpenSnapServer(cameraParam.CapturePort);
                }
            }
            catch (Exception e)
            {
                throw new CameraException("无法登陆摄像头", e.Message);
            }

            return ret;
        }

        public bool Play(Camyu cameraParam)
        {
            throw new NotImplementedException();
        }

        public bool Stop(Camyu cameraParam)
        {
            throw new NotImplementedException();
        }

        public bool Close(Camyu cameraParam)
        {
            bool ret = false;

            try
            {
                // 关闭摄像头
                CamyuDll.KsCamera_Camyu_Logout(cameraParam.DevHandle);
                cameraParam.DevHandle = IntPtr.Zero;
                ret = true;
            }
            catch (Exception e)
            {
                throw new CameraException("无法登出摄像头", e.Message);
            }

            return ret;
        }

        public bool Capture(Camyu cameraParam, string strImageFile)
        {
            bool ret = false;

            try
            {
                if (cameraParam.RealCapture)
                {
                    TriggerCaptureAsync(cameraParam, strImageFile);
                    ret = true;
                }
                else
                {
                    ret = CamyuDll.KsCamera_Camyu_Capture(cameraParam.DevHandle, strImageFile);
                }
            }
            catch (Exception e)
            {
                throw new CameraException("抓拍失败", e.Message);
            }

            return ret;
        }

        public bool OpenSnapServer(ushort nPort = 9999)
        {
            bool ret = false;
            try
            {
                ret = CamyuDll.KsCamera_Camyu_OpenSnapServer(nPort);
            }
            catch (Exception e)
            {
                throw new CameraException("无法打开抓拍监听服务", e.Message);
            }

            return ret;
        }

        /// <summary>
        /// 关闭抓拍监听服务
        /// <para>因为可能还有其他的摄像头在实时抓拍，所以抓拍监听服务应该在注销SDK前关闭</para>
        /// <para>所以该函数为私有函数</para>
        /// </summary>
        void CloseSnapServer()
        {
            try
            {
                CamyuDll.KsCamera_Camyu_CloseSnapServer();
            }
            catch (Exception e)
            {
                throw new CameraException("无法关闭抓拍监听服务", e.Message);
            }
        }

        public void TriggerCaptureAsync(Camyu cameraParam, string strImageFile)
        {
            try
            {
                CamyuDll.KsCamera_Camyu_TriggerCaptureAsync(cameraParam.DevHandle, strImageFile, cameraParam.FlashChannel);
            }
            catch (Exception e)
            {
                throw new CameraException("触发抓拍失败", e.Message);
            }
        }

        public void Timing(Camyu cameraParam, int nYear, int nMonth, int nDay, int nHour, int nMinute, int nSecond)
        {
            try
            {
                CamyuDll.KsCamera_Camyu_Timing(cameraParam.DevHandle, nYear, nMonth, nDay, nHour, nMinute, nSecond);
            }
            catch (Exception e)
            {
                throw new CameraException("校时失败", e.Message);
            }
        }
    }
}
